package com.zlc.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 第三方服务调用工具类
 */
@Slf4j
@Component
public class RestTemplateUtils {

    private static RestTemplate restTemplate;

    public static RestTemplate getRestTemplate() {
        return restTemplate;
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        RestTemplateUtils.restTemplate = restTemplate;
    }

    public static String get(String url,Map<String,Object> variables) {

        return restTemplate.getForObject(url, String.class,variables);
    }


    public static String postExchange(String url, String body) {
        try {
            HttpHeaders header = new HttpHeaders();
            header.set("Content-Type", "application/json; charset=UTF-8");

            HttpEntity<String> entity = new HttpEntity<>(body, header);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
            if (HttpStatus.OK.equals(response.getStatusCode())) {
                return response.getBody();
            } else {
                log.error("调用外部接口失败，url:{}", url);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String getExchange(String url,Map<String,Object> variables) {

        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            HttpHeaders headers = new HttpHeaders();
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                String token = request.getHeader("Authorization");
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.set("Authorization", token);
                headers.set("Content-Type","application/x-www-form-urlencoded");
                headers.set("Accept","application/json, text/plain, */*");
            }

            HttpEntity<String> entity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class,variables);
            if (HttpStatus.OK.equals(response.getStatusCode())) {
                return response.getBody();
            } else {
                log.error("调用外部接口失败，url -> {}", url);
            }

        } catch (Exception ex) {
            log.error("调用外部接口失败，url -> {}", url);
            ex.printStackTrace();
        }
        return null;
    }

}
